package com.evil.application.pojo.dto.view.config.filter;

import com.alibaba.fastjson.JSON;
import com.evil.application.pojo.dto.form.control.ControlConditionRespDTO;
import com.evil.application.pojo.dto.form.control.option.FormOptionRespDTO;
import com.evil.application.pojo.entity.ApplicationFilterItem;
import com.evil.common.application.dto.order.data.ControlDataValueDTO;
import com.evil.common.application.enums.judge.JudgeSignEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * 筛选视图-条件组
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
public class FilterGroupConfigDTO {

    /**
     * 条件id
     */
    private Long filterItemId;

    /**
     * 条件名
     */
    @NotBlank(message = "筛选视图 条件名不能为空")
    private String filterItemName;

    /**
     * 默认值判断符号 {@link JudgeSignEnum}
     */
    private Integer judgeSign;

    /**
     * 默认值可用判断符号
     */
    private List<ControlConditionRespDTO> judgeSigns;

    /**
     * 默认值
     */
    private List<ControlDataValueDTO> defaultValue;

    /**
     * 默认值下拉项
     */
    private List<FormOptionRespDTO> options;

    /**
     * 报表范围 条件配置
     */
    @NotEmpty(message = "筛选视图 条件配置不能为空")
    private List<FilterConditionDTO> filterCondition;

    public FilterGroupConfigDTO() {
        this.judgeSigns = new ArrayList<>();
        this.defaultValue = new ArrayList<>();
        this.options = new ArrayList<>();
        this.filterCondition = new ArrayList<>();
    }

    public FilterGroupConfigDTO(ApplicationFilterItem item) {
        this.filterItemId = item.getFilterItemId();
        this.filterItemName = item.getFilterItemName();
        this.judgeSign = item.getJudgeSign();
        this.defaultValue = new ArrayList<>();
        this.judgeSigns = new ArrayList<>();
        this.filterCondition = new ArrayList<>();
        this.options = new ArrayList<>();

        if (StringUtils.isNotBlank(item.getDefaultValue())) {
            this.defaultValue = JSON.parseArray(item.getDefaultValue(), ControlDataValueDTO.class);
        }
        if (StringUtils.isNotBlank(item.getJudgeSigns())) {
            this.judgeSigns = JSON.parseArray(item.getJudgeSigns(), ControlConditionRespDTO.class);
        }
        if (StringUtils.isNotBlank(item.getFilterCondition())) {
            this.filterCondition = JSON.parseArray(item.getFilterCondition(), FilterConditionDTO.class);
        }
        if (StringUtils.isNotBlank(item.getOptions())) {
            this.options = JSON.parseArray(item.getOptions(), FormOptionRespDTO.class);
        }
    }

    public void copyProp(ApplicationFilterItem item) {
        item.setFilterItemName(this.filterItemName);
        item.setJudgeSign(this.judgeSign);

        item.setDefaultValue(JSON.toJSONString(this.defaultValue));
        item.setJudgeSigns(JSON.toJSONString(this.judgeSigns));
        item.setFilterCondition(JSON.toJSONString(this.filterCondition));
        item.setOptions(JSON.toJSONString(this.options));
    }
}
